// commpents/bottomGuide.js

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    number: {
      type: Number,
      value: 0
    },
    price: {
      type: Number,
      value: 0
    },
    beauticianInfo: {
      type: Object,
      value: {}
    },
    bgIsShow: {
      type: Boolean,
      value: false
    },
    bottom: {
      type: Number,
      value: 0
    },
    url: {
      type: String,
      value: ''
    },
    list:{
      type: Array,
      value: []
    }
  },
  /**
   * 组件的初始数据
   */
  data: { 
    Number: 0, // 商品总数量
    Price: 0, // 总价格
    BeauticianInfo: {}, // 美容师的信息 
    BgIsShow: false, // 背景是否显示
    projectNumber: 0,
    Bottom: 0, // 位置
    Url: '', // 需要跳转的链接
    List: [], // 项目总数（数组对象）
    Height: '',// 购物车高度
  },
  // 属性监听器
  observers:{
    'number,price,beauticianInfo,bottom,url,list'(number,price,beauticianInfo,bottom,url,list){
      this.setData({Number: number, Price: price, BeauticianInfo: beauticianInfo, Bottom: bottom, Url: url,List: list});
    },
    'bgIsShow'(bgIsShow){
      this.data({BgIsShow: bgIsShow});
    }
  },
  // 组件的生命周期
  lifetimes: {
    created: function(){
     this.animation = wx.createAnimation({duration: 400,timingFunction: 'ease'});
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 显示隐藏背景
    Ishow_bg: function(){
      this.setData({BgIsShow: !this.data.BgIsShow})
      // 返回给调用的组件
      this.triggerEvent('callFunction', this.data.BgIsShow);
      if(this.data.BgIsShow){
        this.setShoppingHeight();
      }else{
        this.animation.height("0rpx").step();
      }
      this.setData({ani: this.animation.export()})
    },
    // tourl 跳转下一步
    tourl: function(){ 
      let url = '',Url = '', obj = {}; 
      if(this.data.Url != ''){
        url = this.data.Url.split('?');
        Url = url[0];
        obj = JSON.parse(url[1]);
        let activeBeautician = Object.keys(obj.activeBeautician).length; // 是否选中美容师
        if(activeBeautician !=0 && obj.List.length > 0){
          this.triggerEvent('toConfirmTime',{idx: 1}); // 跳转确认时间
        }else{
          if(activeBeautician < 1 && obj.List.length > 0){ // 美容师 为空
            this.triggerEvent('toIdx',{idx: 0});
            return;
          }else{
            this.triggerEvent('toIdx',{idx: 1});
            return;
          }
        }
      }else{
        console.error("url为空");
      }
    },
    // 负责跳转选择美容师列表
    tourlBeautician: function(){
      this.triggerEvent('toIdx',{idx: 0});// 项目详情转到美容师列表 
    },
    // 获取选择购物车选项 0=>减 1=>加
    ProjectCarOpction: function(Obj){
      let data = Obj.target.dataset,
          btn = data.btn, obj = data.obj,
          List = this.data.List;
      if(btn===""||btn===undefined){return;}
      if(btn == 0){
        for(var key in List){
          if(obj.id == List[key].id){
            List[key].number = List[key].number - 1;
            this.triggerEvent('ProjectCarOpction',{btn: btn, list: List[key]}); //传回需要操作的id
          }
        }
      }else{
        for(var key in List){
          if(obj.id == List[key].id){
            List[key].number = List[key].number + 1;
            this.triggerEvent('ProjectCarOpction',{btn: btn, list: List[key]}); //传回需要操作的id
          }
        }
      }
      this.setData({List: this.data.List});
      if(this.data.List.length==0){this.Ishow_bg()};
      this.setShoppingHeight();
    },
    // 分配高度给购物车
    setShoppingHeight: function(){
      var self = this;
      this.animation.height('auto').step();
      if(self.data.List.length == 0){return;}
      this.createSelectorQuery().select('.footer-show-list').boundingClientRect().selectViewport().scrollOffset().
      exec((res)=>{
        if(self.data.List.length >= 4){
          self.setData({Height: res[0].height * 3});
        }else{
          self.setData({Height: res[0].height * self.data.List.length});
        }
      })
    },
    // 清空购物车
    clearShoppingCar: function(){
      console.log("清空购物车");
      this.setData({List: [], BgIsShow: false});
      this.triggerEvent('callFunction', this.data.BgIsShow); // 关闭渲染
      this.triggerEvent('ProjectCarOpction', 0); // 清空已选择数量列表
    }
  }
})
